package com.hospital.new_yygh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hospital.new_yygh.entity.other.Order;
import com.hospital.new_yygh.entity.other.Sche;
import com.hospital.new_yygh.entity.result.Result;
import com.hospital.new_yygh.entity.vo.OrderVo;
import com.hospital.new_yygh.service.OrderService;
import com.hospital.new_yygh.service.ScheService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Api(tags = "预约表")
@RestController
@RequestMapping("/order")
@CrossOrigin
public class OrderController {
    //注入service
    @Autowired
    private OrderService orderService;

    @Autowired
    private ScheService scheService;


    //分页查询预约信息
    @ApiOperation(value = "分页查询预约信息")
    @PostMapping("getOrderByksId/{current}/{limit}")
    public Result getOrderByksId(@PathVariable long current,
                                 @PathVariable long limit,
                                 @RequestBody(required = false) OrderVo orderVo){
        //创建page对象，传递当前页，每页记录数
        Page<Order> page = new Page<>(current,limit);
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        String doctorName = orderVo.getDoctorName();
        String patientName = orderVo.getPatientName();
        String ksId = orderVo.getKsId();
        Long doctorId = orderVo.getDoctorId();
        Long patientId = orderVo.getPatientId();
        String orderTime = orderVo.getOrderTime();

        if (!StringUtils.isEmpty(doctorName)){
            wrapper.like("doctorName",doctorName);
        }
        if (!StringUtils.isEmpty(patientName)){
            wrapper.like("patientName",patientName);
        }
        if (!StringUtils.isEmpty(ksId)){
            wrapper.eq("ksId",ksId);
        }
        if (!StringUtils.isEmpty(doctorId)){
            wrapper.eq("doctor_id",doctorId);
        }
        if (!StringUtils.isEmpty(patientId)){
            wrapper.eq("patient_id",patientId);
        }
        if (!StringUtils.isEmpty(orderTime)){
            wrapper.eq("order_time",orderTime);
        }
        Page<Order> pageOrder = orderService.page(page, wrapper);
        return Result.ok(pageOrder);
    }




    //根据排班id预约挂号
    @ApiOperation(value = "预约挂号")
    @PostMapping("patientOrder/{id}")
    public Result patientOrder(@PathVariable long id,
                                @RequestBody(required = false) Order order) throws ParseException {
        //调用scheController中的getSche/{id}方法获取排班信息
        Sche sche = scheService.getById(id);
        order.setDoctorId(sche.getDoctorId());
        order.setDoctorName(sche.getDoctorName());
        order.setKsId(sche.getKsId());
        order.setKsName(sche.getKsName());
        order.setStatus(1);
        order.setOrderTime(sche.getWorkDate());
        order.setWorkTime(sche.getWorkTime());
        boolean save = orderService.save(order);
        if (save){
            //挂号成功 医生排班可预约数-1
            //医生id
            Long doctorId = order.getDoctorId();
            //医生日期
            String orderTime = order.getOrderTime();
            //医生时间 上下午
            Integer workTime = order.getWorkTime();

            QueryWrapper<Sche> wrapper = new QueryWrapper<>();
            wrapper.eq("doctor_id",doctorId);
            wrapper.eq("work_date",orderTime);
            wrapper.eq("work_time",workTime);
            Sche one = scheService.getOne(wrapper);
            //剩余预约数-1
            Integer availableNumber = one.getAvailableNumber();
            one.setAvailableNumber(availableNumber--);
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    //查看患者个人的预约信息
    /*@ApiOperation(value = "查看患者自己的预约信息")
    @PostMapping("getOrderByPatientId/{patientId}/{current}/{limit}/{}")
    public Result getOrderByPatientId(@PathVariable long current,
                                      @PathVariable long limit,
                                      @PathVariable long patientId){
        Page<Order> page = new Page<>(current,limit);
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("patient_id",patientId);
        List<Order> list = orderService.list(wrapper);
        Page<Order> pageOrder = orderService.page(page, wrapper);
        return Result.ok(list);
    }*/

    //查看医生个人的预约信息
   /* @ApiOperation(value = "查看医生个人的预约信息")
    @PostMapping("getOrderByDoctorId/{doctorId}/{current}/{limit}")
    public Result getOrderByDoctorId(@PathVariable long current,
                                     @PathVariable long limit,
                                     @PathVariable long doctorId){
        Page<Order> page = new Page<>(current,limit);
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("doctor_id",doctorId);
        List<Order> list = orderService.list(wrapper);
        return Result.ok(list);
    }*/

    //根据id删除挂号信息
    @ApiOperation(value = "根据id删除挂号信息")
    @DeleteMapping("removeOrder/{id}")
    public Result removeOrder(@PathVariable Long id){
        boolean flag = orderService.removeById(id);
        if (flag){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    //根据id修改预约状态
    @ApiOperation(value = "根据id修改预约状态")
    @PostMapping("dealOrderById/{id}/{status}")
    public Result dealOrderById(@PathVariable Long id,
                                @PathVariable Integer status){
        //根据id查询预约
        Order order = orderService.getById(id);
        order.setStatus(status);
        //调用方法
        orderService.updateById(order);
        return Result.ok();
    }

    //医生添加预约挂号
    @ApiOperation(value = "医生添加预约挂号")
    @PostMapping("doctorOrder")
    public Result doctorOrder(@RequestBody(required = false) Order order){
        order.setStatus(1);
        boolean save = orderService.save(order);
        if (save){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }
}
